<template>
	<view>
		<view class="search-box">
			<my-search @click="goToSearch"></my-search>
		</view>
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="item in swiperList">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类导航区 -->
		<view class="nav-list">
			<image v-for="item in navList" :src="item.image_src" class="nav-img" @click="navClickHandler(item)"></image>
		</view>
		<!-- 楼层展示区 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item,i) in floorList" :key="i">
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<view class="floor-img">
					<navigator class="floor-img-left" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>
					<view class="floor-img-right">
						<navigator class="floor-img-item" v-for="(item2,i2) in item.product_list" :key="i2"
							v-if="i2 !== 0" :url="item2.url">
							<image :src="item2.image_src" :style="{width: item2.image_width+'rpx'}" mode="widthFix">
							</image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	//引用封装好的购物车角标方法
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				//轮播图的数据列表
				swiperList: [],
				// 分类导航的数据列表
				navList: [{
						image_src: "../../static/fenlei.png",
						name: "fenlei"
					},
					{
						image_src: "../../static/miaoshapai.png",
						name: "miaoshapai"
					},
					{
						image_src: "../../static/chaoshigou.png",
						name: "chaoshigou"
					},
					{
						image_src: "../../static/muyingping.png",
						name: "muyingping"
					}
				],
				//楼层的数据
				floorList: []
			};
		},
		onLoad() {
			this.getSwiperList()
			this.getFloorList()
		},
		methods: {
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.swiperList = res.message

			},
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()

				res.message.forEach(floor => {
					floor.product_list.forEach(prod => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
			},
			navClickHandler(item) {
				if (item.name === 'fenlei') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				} else if (item.name === 'miaoshapai') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				} else if (item.name === 'chaoshigou') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				} else if (item.name === 'muyingping') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			goToSearch() {
				uni.navigateTo({
					url: '../../subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
			margin: 0 26rpx;
		}
	}

	.floor-title {
		width: 100%;
		height: 60rpx;
		display: flex;
	}

	.floor-img-right {
		display: flex;
		flex-wrap: wrap;
	}

	.floor-img {
		display: flex;
	}

	.search-box {
		//设置吸顶效果，使搜索框始终在页面顶部
		position: sticky;
		//设置高度
		top: 0;
		//设置层级，防止别的东西覆盖搜索框
		z-index: 999;
	}
</style>
